package com.xuyiwei.mayi.test;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by xuyiwei on 2019/5/6.
 */
public class CglibDemo1 implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib代理开始");
        //这里要invokeSuper
        Object obj = methodProxy.invokeSuper(o,objects);
        System.out.println("cglib代理结束");

        return obj;
    }

    public static void main(String[] args) {
        CglibDemo1 cg = new CglibDemo1();
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(cg);
        enhancer.setSuperclass(HoseImpl.class);
        Hose hose = (Hose) enhancer.create();
        hose.mai();
    }
}
